//
//  StoreHomeViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/3.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "StoreHomeViewController.h"
#import "TB_CollectionView.h"
#import "HomeCollectionViewCell.h"
#import "ShopInforModel.h"
#import "StoreHomeCollectionReusableView.h"
#import "INAcountEditViewController.h"
#import "OutAccountViewController.h"
#import "CheckRecordViewController.h"
#import "PeopleManagerViewController.h"
#import "SupplyManagerViewController.h"
#import "GoodManagerViewController.h"
#import "GetRegisterInforModel.h"
#import "ShopRegisterViewController.h"
#import "PatrolListViewController.h"
#import "LookInforViewController.h"
#import "ItemReprotViewController.h"
#import "GotoCheckViewController.h"
#import "SubjectListViewController.h"
#import "RecruitViewController.h"
@interface StoreHomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)TB_CollectionView *homeCollectionView;

@property(nonatomic,strong)NSMutableArray *dataArr;

@property(nonatomic,strong)ShopInforModel *shopInforModel;

@end

@implementation StoreHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.homeCollectionView];
    
    [self loadDatas];
    // Do any additional setup after loading the view.
    
    if ([self getUserType]==2) {
        
        [self CreatHomeBtnWithName:@"去检查" isStoreHome:YES];
        
    }
}


#pragma mark-检查员去检查

-(void)GotoHome:(UIButton*)sender
{
    [sender.layer pop_removeAllAnimations];
    
    //去检查
    
    JobListModel *list=[JobListModel new];
    
    list.type= [NSString stringWithFormat:@"%zd",self.shopInforModel.data.type];
    
    list.ptype=@"1";
    
    SubjectListViewController *shopsubject=[SubjectListViewController new];
    
    shopsubject.reqModel=list;
    
    shopsubject.Tiptitle=self.shopInforModel.data.shopName;
    
    shopsubject.objId=[NSString stringWithFormat:@"%ld",self.shopid];
    
    [self.navigationController pushViewController:shopsubject animated:YES];
}

-(NSMutableArray*)dataArr
{
    if (!_dataArr) {
        
        _dataArr=[NSMutableArray array];
        
    }
    
    return _dataArr;
}


-(TB_CollectionView*)homeCollectionView
{
    if (!_homeCollectionView) {
  
        UICollectionViewFlowLayout *layOut=[[UICollectionViewFlowLayout alloc]init];
        
        layOut.scrollDirection=UICollectionViewScrollDirectionVertical;//垂直滚动
        
        layOut.sectionInset=UIEdgeInsetsMake(0, 0, 0, 0);
        
        _homeCollectionView=[[TB_CollectionView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:layOut];

        _homeCollectionView.delegate=self;
        
        _homeCollectionView.dataSource=self;

        [_homeCollectionView registerNib:[UINib nibWithNibName:@"HomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
        [_homeCollectionView registerClass:[StoreHomeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        
    }
    
    return _homeCollectionView;
    
}





-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/shopInfo.json" andrequestTime:5 andParams:@{@"shopId":[NSString stringWithFormat:@"%ld",self.shopid]} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        self.shopInforModel=[ShopInforModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
         
            [self.dataArr addObjectsFromArray:self.shopInforModel.data.menus];
            
            [self.homeCollectionView reloadData];
         
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
        
    }];

}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArr.count;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShopInforMenus *meaues=self.dataArr[indexPath.row];
    
    HomeCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.funNameLabel.text=meaues.name;
    
    cell.funImageLabel.image=[UIImage imageNamed:meaues.icon];

    return cell;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 1;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 1;
    
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size=CGSizeMake((SCREEN_WIDTH-2)/3.0, (SCREEN_WIDTH-2)/3.0);
    
    return size;
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    return CGSizeMake(SCREEN_WIDTH, 200);
    
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        
        StoreHomeCollectionReusableView *head=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        
        head.imageUrl=self.shopInforModel.data.shopImg;
        
        head.name=self.shopInforModel.data.shopName;
        
        
        return head;
        
    }
    
    return nil;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
  ShopInforMenus *meaues=self.dataArr[indexPath.row];
    
    switch (meaues.ID) {
        case 1:
        {
            //进货台账
            INAcountEditViewController *inAE=[INAcountEditViewController new];
            
            inAE.Lookshopid=self.shopid;
            
            [self.navigationController pushViewController:inAE animated:YES];
            
        }
            break;
            
        case 2:
        {
            //出货台账
            
            JobListModel *list=[JobListModel new];
            
            OutAccountViewController *OutAcount=[OutAccountViewController new];
          
            OutAcount.isEdit=NO;
                
            OutAcount.isdeleMore=NO;
                
            list.shopId=self.shopid;
        
            OutAcount.AddBtnTitle=@"新增出货";
            
            OutAcount.searchName=@"输入关键字";
            
            OutAcount.title=@"出货台账";
            
            OutAcount.Reqmodel=list;
            
            [self.navigationController pushViewController:OutAcount animated:YES];
            
            
        }
            break;
        case 3:
        {
            //检查记录
            
            
            JobListModel *list=[JobListModel new];
            
            CheckRecordViewController *CheckR=[CheckRecordViewController new];
            
            CheckR.isEdit=NO;
            
            CheckR.isdeleMore=NO;
      
            list.shopId=self.shopid;
                
            list.ptype=@"1";
                
            list.type=[NSString stringWithFormat:@"%ld",self.shopInforModel.data.type];
     
            CheckR.searchName=@"输入关键字";
            
            CheckR.Reqmodel=list;
            
            CheckR.title=@"检查记录";
            
            CheckR.isAutoLoding=YES;
            
            [self.navigationController pushViewController:CheckR animated:YES];
            
            
        }
            break;
            
        case 4:
        {
            //员工管理
            
            
            PeopleManagerViewController *People=[PeopleManagerViewController new];
            
            People.title=meaues.name;
            
            People.shopid=self.shopid;
            
            [self.navigationController pushViewController:People animated:YES];
            
            
        }
            break;
        case 5:
        {
            //供应商管理
            
            JobListModel *list=[JobListModel new];
            SupplyManagerViewController *Supply=[SupplyManagerViewController new];
            Supply.isEdit= [LoginStatuesModel getUserType]<3?YES:NO;
            Supply.isdeleMore=NO;
            list.shopId=self.shopid;
            Supply.Reqmodel=list;
            Supply.searchName=@"输入供应商名称";
            Supply.AddBtnTitle=@"新增供应商";
            Supply.title=@"供应商列表";
            [self.navigationController pushViewController:Supply animated:YES];
            
            
        }
            break;
        case 6:
        {
            //商品管理
            
            GoodManagerViewController *Goods=[GoodManagerViewController new];
            
            Goods.shopid=self.shopid;
            
            [self.navigationController pushViewController:Goods animated:YES];
    
        }
            break;
            
        case 7:
        {
            //资质档案
            
//            if ([self getUserType]==4) {
//
//                LookInforViewController *Infor=[LookInforViewController new];
//
//                Infor.reqUrl=@"/index.php/FoodCheckApi/shopDetail.json";
//
//                Infor.params=@{@"shopId":[NSString stringWithFormat:@"%ld",self.shopid]};
//
//                Infor.title=@"商户详情";
//
//                [self.navigationController pushViewController:Infor animated:YES];
//
//            }else{
            
            GetRegisterInforModel *shopmodel=[GetRegisterInforModel new];
            
            shopmodel.type=[NSString stringWithFormat:@"%ld",self.shopInforModel.data.type];
            
            shopmodel.shopId=[NSString stringWithFormat:@"%ld",self.shopid];
            
            shopmodel.token=[LoginStatuesModel getUserToken];
            
            ShopRegisterViewController *Edit=[ShopRegisterViewController new];
            
            Edit.title=@"商户资质";
            
            Edit.BtnStr=@"保存";
            
            Edit.OptionSussStr=@"保存成功";
            
            Edit.requrl=@"/index.php/FoodCheckApi/shopRegInfo.json";
            
            Edit.Reqmodel=shopmodel;
            
            Edit.postUrl=@"/index.php/FoodCheckApi/shopReg.json";
            
            Edit.block=^()
            {
                
                
            };
            
            [self.navigationController pushViewController:Edit animated:YES];
  
//            }
                
        }
            break;
            
        case 10:
        {
            ItemReprotViewController *ItemReport=[ItemReprotViewController new];
            
            ItemReport.shopid=self.shopid;
            
            [self.navigationController pushViewController:ItemReport animated:YES];
            
        }
            break;
            
            case 11:
        {
            
            JobListModel *list=[JobListModel new];
            
            PatrolListViewController *Patro=[PatrolListViewController new];
            
            if ([self getUserType]==3) {
               
                Patro.isEdit=NO;
                
                Patro.isdeleMore=NO;
                
                Patro.isGroupStyle=YES;
                
            }else{
            
            Patro.isEdit=YES;
            
            Patro.isdeleMore=YES;
            
             Patro.isGroupStyle=YES;
            
            }
            list.type=[NSString stringWithFormat:@"%ld",self.shopInforModel.data.type];
            
            list.ptype=@"1";
            
            list.shopId=self.shopid;
            
            Patro.Reqmodel=list;
            
            Patro.shopName=self.shopInforModel.data.shopName;
            
            Patro.searchName=@"输入日志";
            Patro.AddBtnTitle=@"新增日志";
            Patro.title=@"检查日志";
            [self.navigationController pushViewController:Patro animated:YES];
            
        }
            break;
        case 12://招聘
        {
            RecruitViewController *Patro=[RecruitViewController new];
            JobListModel *list=[JobListModel new];
            list.pageOn = YES;
            list.pageSize = 10;
            list.currentPage = 1;
            
            Patro.ishidenSearch = NO;
            Patro.Reqmodel = list;
            Patro.isEdit= [LoginStatuesModel getUserType]==1?YES:NO;
            Patro.isdeleMore=YES;
            Patro.isGroupStyle=YES;
            Patro.searchName=@"输入职位名称";
            Patro.AddBtnTitle=@"新增招聘";
            Patro.title=@"招聘管理";
            [self.navigationController pushViewController:Patro animated:YES];
        }
            break;
        default:
        
            break;
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
